package com.ruoyi.web.service.screen.impl;

import com.ruoyi.web.domain.screen.BigScreen;
import com.ruoyi.web.mapper.screen.BigScreenMapper;
import com.ruoyi.web.pojo.dto.screen.BigScreenRep;
import com.ruoyi.web.service.screen.IBigScreenService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 大屏配置Service业务层处理
 * 
 * @author zq
 * &#064;date  2024-01-15
 */
@Service
public class BigScreenServiceImpl implements IBigScreenService
{

    private final BigScreenMapper bigScreenMapper;

    public BigScreenServiceImpl(BigScreenMapper bigScreenMapper) {
        this.bigScreenMapper = bigScreenMapper;
    }

    /**
     * 查询大屏配置
     * 
     * @param id 大屏配置主键
     * @return 大屏配置
     */
    @Override
    public BigScreenRep selectBigScreenById(Long id)
    {
        BigScreen bigScreen = bigScreenMapper.selectBigScreenById(id);
        if (Objects.isNull(bigScreen)){
            return new  BigScreenRep();
        }

        return BigScreenRep.toBigScreenRep(bigScreen);
    }

    /**
     * 查询大屏配置列表
     * 
     * @param bigScreen 大屏配置
     * @return 大屏配置
     */
    @Override
    public List<BigScreenRep> selectBigScreenList(BigScreen bigScreen)
    {
        List<BigScreen> bigScreens = bigScreenMapper.selectBigScreenList(bigScreen);
        if (CollectionUtils.isEmpty(bigScreens)){
            return  new ArrayList<>();
        }
        return bigScreens.stream().map(BigScreenRep::new).collect(Collectors.toList());
    }

    /**
     * 新增大屏配置
     * 
     * @param bigScreen 大屏配置
     * @return 结果
     */
    @Override
    public int insertBigScreen(BigScreen bigScreen)
    {
        if ("0".equals(bigScreen.getState())){
            extracted(bigScreen);
        }
        return bigScreenMapper.insertBigScreen(bigScreen);
    }

    private void extracted(BigScreen bigScreen) {
        BigScreen big = new BigScreen();
        big.setType(bigScreen.getType());
        List<BigScreen> bigScreens = bigScreenMapper.selectBigScreenList(big);
        for (BigScreen screen : bigScreens){
            screen.setState("1");
            bigScreenMapper.updateBigScreen(screen);
        }
    }

    /**
     * 修改大屏配置
     * 
     * @param bigScreen 大屏配置
     * @return 结果
     */
    @Override
    public int updateBigScreen(BigScreen bigScreen)
    {
        if ("0".equals(bigScreen.getState())){
            extracted(bigScreen);
        }
        return bigScreenMapper.updateBigScreen(bigScreen);
    }

    /**
     * 批量删除大屏配置
     * 
     * @param ids 需要删除的大屏配置主键
     * @return 结果
     */
    @Override
    public int deleteBigScreenByIds(Long[] ids)
    {
        return bigScreenMapper.deleteBigScreenByIds(ids);
    }

    /**
     * 删除大屏配置信息
     * 
     * @param id 大屏配置主键
     * @return 结果
     */
    @Override
    public int deleteBigScreenById(Long id)
    {
        return bigScreenMapper.deleteBigScreenById(id);
    }
}
